package com.ca.ridersclubapplication;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ca.ridersclubapplication.adapter.AccountAdapter;
import com.ca.ridersclubapplication.request.AccountRequester;
import com.ca.ridersclubapplication.request.AfterExecutor;
import com.ca.ridersclubapplication.request.ClubRequester;
import com.ca.ridersclubapplication.request.dto.AccountCardDto;
import com.ca.ridersclubapplication.request.dto.AccountDto;
import com.ca.ridersclubapplication.request.dto.Result;

import java.util.List;

public class AccountListActivity extends ImmersiveActivity {

    private Long accountId;
    private String type;
    private boolean isClubFans;
    private Long clubId;

    private ImageView backView;
    private TextView titleView;
    private RecyclerView rv;
    private AccountAdapter adapter;


    private ProgressBar progressBar;
    private Long startId = Long.MAX_VALUE;
    private boolean canRequest = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_list);

        initData();

        initView();

        initListener();

        loadData();
    }

    private void initData() {
        accountId = getIntent().getLongExtra("accountId", 0L);
        clubId = getIntent().getLongExtra("clubId", 0L);
        type = getIntent().getStringExtra("type");

        if (accountId == 0L && clubId == 0L) {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            finishAfterTransition();
        }

        if (!"followList".equals(type) && !"fansList".equals(type) && !"ClubFansList".equals(type)) {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            finishAfterTransition();
        }

        isClubFans = "ClubFansList".equals(type);
    }

    private void initView() {

        backView = findViewById(R.id.iv_back);
        titleView = findViewById(R.id.tv_title);

        if (isClubFans) {
            titleView.setText("用户列表");
        }
        else if ("followList".equals(type)) {
            titleView.setText("关注列表");
        }
        else {
            titleView.setText("粉丝列表");
        }

        rv = findViewById(R.id.rv);
        adapter = new AccountAdapter(this, null);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);

        progressBar = findViewById(R.id.bar_progress);
    }

    private void initListener() {

        backView.setOnClickListener(v -> {
            finishAfterTransition();
        });
    }

    private void loadData() {

        if (isClubFans) {
            listClubFans();
        }
        else if ("followList".equals(type)) {
            listFollow();
        }
        else {
            listFans();
        }
    }

    private synchronized void listFollow() {
        if (canRequest) {
            canRequest = false;
            //  显示加载动画
            progressBar.setVisibility(View.VISIBLE);

            //  请求数据
            AccountRequester.listFollow(AccountListActivity.this, accountId, startId, new AfterExecutor<List<AccountCardDto>>() {
                @Override
                public void execute(Result<List<AccountCardDto>> result) {
                    List<AccountCardDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        //  请求成功，设置视图数据
                        adapter.appendListToBottom(data);

                        Log.e("开始ID", startId + "");

                        //  设置下一个查询的开始ID
                        if (data.size() > 0) {
                            startId = data.get(data.size() - 1).getId();
                        } else {
                            Toast.makeText(AccountListActivity.this, "已经到底咯~", Toast.LENGTH_SHORT).show();
                        }
                        Log.e("下一次开始ID", startId + "");

                        //  关闭加载动画
                        progressBar.setVisibility(View.GONE);
                    }

                    //  允许请求
                    canRequest = true;
                }
            });
        }
        else {
            canRequest = true;
        }
    }

    private synchronized void listFans() {
        if (canRequest) {
            canRequest = false;
            //  显示加载动画
            progressBar.setVisibility(View.VISIBLE);

            //  请求数据
            AccountRequester.listFans(AccountListActivity.this, accountId, startId, new AfterExecutor<List<AccountCardDto>>() {
                @Override
                public void execute(Result<List<AccountCardDto>> result) {
                    List<AccountCardDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        //  请求成功，设置视图数据
                        adapter.appendListToBottom(data);

                        //  设置下一个查询的开始ID
                        if (data.size() > 0) {
                            startId = data.get(data.size() - 1).getId();
                        } else {
                            Toast.makeText(AccountListActivity.this, "已经到底咯~", Toast.LENGTH_SHORT).show();
                        }

                        //  关闭加载动画
                        progressBar.setVisibility(View.GONE);
                    }

                    //  允许请求
                    canRequest = true;
                }
            });
        }
        else {
            canRequest = true;
        }
    }

    private synchronized void listClubFans() {
        if (canRequest) {
            canRequest = false;
            //  显示加载动画
            progressBar.setVisibility(View.VISIBLE);

            //  请求数据
            ClubRequester.listFans(this, clubId, new AfterExecutor<List<AccountCardDto>>() {
                @Override
                public void execute(Result<List<AccountCardDto>> result) {
                    List<AccountCardDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        adapter.setList(data);
                    }
                }
            });
        }
        else {
            canRequest = true;
        }
    }
}